package com.hgyc.mom.wms.service.impl;

import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hgyc.mom.common.enums.CommonOrderStatusEnum;
import com.hgyc.mom.common.enums.FinishTypeEnum;
import com.hgyc.mom.common.exception.BizException;
import com.hgyc.mom.tool.strategy.AutoCodeUtil;
import com.hgyc.mom.wms.entity.ScmPurchaseDetail;
import com.hgyc.mom.wms.entity.ScmPurchaseOrder;
import com.hgyc.mom.wms.entity.WmsPurchaseReceipt;
import com.hgyc.mom.wms.enums.PurchaseOrderStatusEnum;
import com.hgyc.mom.wms.enums.PurchaseReceiptStatusEnum;
import com.hgyc.mom.wms.mapper.ScmPurchaseOrderMapper;
import com.hgyc.mom.wms.service.ScmPurchaseDetailService;
import com.hgyc.mom.wms.service.ScmPurchaseOrderService;
import com.hgyc.mom.wms.vo.ScmPurchaseOrderVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;

import java.util.List;

/**
 * 采购订单Service业务层实现
 *
 * @author fwj
 * @since 2025-05-15
 */
@Slf4j
@Service
public class ScmPurchaseOrderServiceImpl extends ServiceImpl<ScmPurchaseOrderMapper, ScmPurchaseOrder> implements ScmPurchaseOrderService {

    @Autowired
    private ScmPurchaseDetailService scmPurchaseDetailService;

    @Autowired
    private AutoCodeUtil autoCodeUtil;

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void savePurChaseOrder(ScmPurchaseOrderVO scmPurchaseOrderVO) {
        scmPurchaseOrderVO.setStatus(CommonOrderStatusEnum.PREPARE.getValue());
        scmPurchaseOrderVO.setPoCode(autoCodeUtil.genSerialCode("PO_CODE", null));
        save(scmPurchaseOrderVO);

        //新增明细
        List<ScmPurchaseDetail> details = scmPurchaseOrderVO.getAddDetails();
        if (!CollectionUtils.isEmpty(details)) {
            details.forEach(scmPurchaseDetail -> {
                scmPurchaseDetail.setId(null);
                scmPurchaseDetail.setPurchaseId(scmPurchaseOrderVO.getId());
                scmPurchaseDetail.setStatus(PurchaseOrderStatusEnum.PREPARE.getValue());
            });
            scmPurchaseDetailService.saveBatch(details);
        }
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void updatePurChaseOrder(ScmPurchaseOrderVO scmPurchaseOrderVO) {
        updateById(scmPurchaseOrderVO);

        //删除明细
        if (!CollectionUtils.isEmpty(scmPurchaseOrderVO.getDeleteDetailIds())) {
            scmPurchaseDetailService.removeBatchByIds(scmPurchaseOrderVO.getDeleteDetailIds());
        }

        //新增
        if (!CollectionUtils.isEmpty(scmPurchaseOrderVO.getAddDetails())) {
            List<ScmPurchaseDetail> addDetails = scmPurchaseOrderVO.getAddDetails();
            addDetails.forEach(detail -> {
                detail.setId(null);
                detail.setPurchaseId(scmPurchaseOrderVO.getId());
            });
            scmPurchaseDetailService.saveBatch(addDetails);
        }

        //更新
        if (!CollectionUtils.isEmpty(scmPurchaseOrderVO.getUpdateDetails())) {
            List<ScmPurchaseDetail> updateDetails = scmPurchaseOrderVO.getUpdateDetails();
            scmPurchaseDetailService.updateBatchById(updateDetails);
        }
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void removePurChaseOrder(List<Long> ids) {
        removeByIds(ids);

        scmPurchaseDetailService.deleteBatchByParentIds(ids);
    }

    @Override
    public void updateStatus(String status, List<Long> ids) {
        LambdaUpdateWrapper<ScmPurchaseOrder> updateWrapper = new LambdaUpdateWrapper<>();
        updateWrapper.in(ScmPurchaseOrder::getId, ids);
        updateWrapper.set(ScmPurchaseOrder::getStatus, status);
        update(updateWrapper);
    }

    /**
     * {@inheritDoc}
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public String importData(List<ScmPurchaseOrder> scmPurchaseOrderList, boolean updateSupport) {
        if (scmPurchaseOrderList == null || scmPurchaseOrderList.isEmpty()) {
            throw new IllegalArgumentException("导入数据不能为空");
        }

        try {
            if (updateSupport) {
                // TODO 实现更新已有数据的导入逻辑
                return "更新导入成功，共导入 " + scmPurchaseOrderList.size() + " 条数据";
            } else {
                // TODO 实现不更新已有数据的导入逻辑
                return "新增导入成功，共导入 " + scmPurchaseOrderList.size() + " 条数据";
            }
        } catch (Exception e) {
            log.error("采购订单导入失败", e);
            throw new BizException("采购订单导入失败：" + e.getMessage());
        }
    }
}
